import argparse
import threading

import yaml
from workers import recycle_worker,power_monitor_worker,upload_worker
from tools.log import logger,setup_logger


def parse_args():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    
    parser.add_argument('--global_conf',
                       required=True,
                       help='全局配置文件路径 (必须存在)')
    
    parser.add_argument('--local_conf',
                       required=True,
                       help='相机配置文件路径 (必须存在)')
    
    parser.add_argument('--conf_encoding',
                       default='utf-8',
                       help='文件编码格式')
    parser.add_argument('--log_file',
                       default=None,
                       help='日志文件')
    return parser.parse_args()

def load_conf(conf_path,conf_encoding):
    with open(conf_path,"r",encoding=conf_encoding) as file:
        return yaml.safe_load(file)    



if __name__=="__main__":
    """
    !!! 确保只有一个线程修改配置文件，涉及图像自增序列号
    """
    args = parse_args()
    setup_logger(args.log_file)
    global_conf = load_conf(args.global_conf,args.conf_encoding)
    local_conf = load_conf(args.local_conf,args.conf_encoding)
    worker_list=[]
    
    worker_list.append(threading.Thread(target=recycle_worker.work,args=(global_conf,local_conf)))
    worker_list.append(threading.Thread(target=power_monitor_worker.work,args=(global_conf,local_conf)))
    worker_list.append(threading.Thread(target=upload_worker.work,args=(global_conf,local_conf)))
    
    for worker in worker_list:
        worker.start()
    
    print("load_ok")
    
    for worker in worker_list:
        worker.join()
    